package com.yc.game.controller;

import com.github.pagehelper.PageInfo;
import com.yc.game.common.Const;
import com.yc.game.common.ServerResponse;
import com.yc.game.pojo.*;
import com.yc.game.service.*;
import com.yc.game.vo.TrainingVo;
import com.yc.game.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TrainingContentService trainingContentService;
    @Autowired
    private TrainingVoService trainingVoService;
    @Autowired
    private RecipesService recipesService;
    @Autowired
    private FoodService foodService;

    /**
     * 跳转到登录或者注册界面
     */
    @RequestMapping("page/loginOrRegister")
    public String loginOrRegisterPage(ModelMap modelMap) {

        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        return "loginOrRegister";
    }

    /**
     * 注册功能
     */
    @RequestMapping("user/register")
    @ResponseBody
    public ServerResponse userRegister(User user) {
        if (user == null) {
            return ServerResponse.createByErrorMessage("注册失败！");
        }
        int count = userService.getByEmail(user.getEmail());
        if (count > 0) {
            return ServerResponse.createByErrorMessage("该邮箱已经被占用！");
        }
        int result = userService.register(user);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("注册成功，请前去登录！");
        }
        return ServerResponse.createByErrorMessage("注册失败！");
    }

    /**
     * 用户登录
     */
    @RequestMapping("user/login")
    @ResponseBody
    public ServerResponse userLogin(User user, HttpServletRequest request) {
        String backUrl = request.getHeader("Referer");
        if (backUrl.equals("") || backUrl.equals("http://localhost:8080/page/loginOrRegister")) {
            backUrl = "/page/index";
        }
        User u = userService.getUserByEmail(user);
        if (u == null) {
            return ServerResponse.createByErrorMessage("账户名或密码错误");
        }
        if (u.getStatus()== 2) {
            return ServerResponse.createByErrorMessage("账号已被禁用");
        }
        request.getSession(true).setAttribute(Const.CURRENT_USER,u);
        return ServerResponse.createBySuccess("登录成功", backUrl);
    }

    /**
     * 跳转到我的个人信息界面
     */
    @RequestMapping("user/info")
    public String userInfo(HttpServletRequest request, ModelMap modelMap,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "loginOrRegister";
        }
        // 查询出基本信息
        UserInfo userInfo = userInfoService.getByEmail(user.getEmail());
        modelMap.addAttribute("userInfo", userInfo);
        // 查询出我发布的信息
        PageInfo<TrainingContent> contentPageInfo = trainingContentService.getByEmail(pageNum, pageSize, user.getEmail());
        modelMap.addAttribute("contentPageInfo", contentPageInfo);
        // 查询出我代练的信息
        PageInfo<TrainingVo> pageInfo = trainingVoService.getByEmail(pageNum, pageSize, user.getEmail());
        modelMap.addAttribute("pageInfo", pageInfo);
        return "info";
    }

    /**
     * 发布代练信息
     */
    @RequestMapping(value = "user/addTraining", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addTraining(HttpServletRequest request, TrainingContent content) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
           return ServerResponse.createByErrorMessage("操作失败，请先登录");
        }
        int result = trainingContentService.add(content, user.getEmail());
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功，等待管理员审核");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    /**
     * 销毁账户信息
     */
    @RequestMapping("user/invalidate")
    public String userInvalidate(HttpServletRequest request, ModelMap modelMap) {
        request.getSession(true).invalidate();
        List<Recipes> recipes = recipesService.getRandom();
        modelMap.addAttribute("recipes", recipes);
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        List<Food> foods = foodService.getByHot();
        modelMap.addAttribute("foods", foods);
        return "index";
    }

}
